<template>
  <el-form ref="deptForm" :model="form" :rules="rules" label-width="120px">
    <el-form-item label="部门名称" prop="name">
      <el-input v-model="form.name" style="width:80%" placeholder="1-50个字符" />
    </el-form-item>
    <el-form-item label="部门编码" prop="code">
      <el-input v-model="form.code" style="width:80%" placeholder="1-50个字符" />
    </el-form-item>
    <el-form-item label="部门负责人" prop="manager">
      <el-select v-model="form.manager" style="width:80%" placeholder="请选择">
        <el-option
          v-for="item in user"
          :key="item.id"
          :value="item.username"
          :label="item.username"
        />
      </el-select>

    </el-form-item>
    <el-form-item label="部门介绍" prop="introduce">
      <el-input v-model="form.introduce" style="width:80%" placeholder="1-300个字符" type="textarea" :rows="3" />
    </el-form-item>
    <el-form-item>
      <el-button type="primary" size="small" @click="submit">确定</el-button>
      <el-button size="small" @click="$emit('hSuccess')">取消</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
import { getEmployess } from '@/api/employess'
import { addDepartments, updataDepartments, changeDepartments } from '@/api/departments'

export default {
  name: 'VueHr2Dialog',
  props: {
    id: {
      type: String,
      required: true
    },
    isEdit: {
      type: Boolean,
      required: true
    },
    orginList: {
      type: Array,
      required: true
    }
  },

  data() {
    // 效验部门编码唯一性
    const validCode = (rule, value, callback) => {
      let newCode = this.orginList.map(item => item.code)
      // console.log(newCode)
      // 编辑状态先移除自己身上的code
      if (this.isEdit) {
        newCode = this.orginList.filter(item => item.id !== this.id).map(item => item.code)
      }
      if (newCode.includes(value)) {
        callback(new Error(`部门编码 ${value} 已存在`))
      } else {
        callback()
      }
    }
    // 效验部门名称唯一性 在同级部门不能重复  通过id来查找
    const validName = (rule, value, callback) => {
      let newName = this.orginList.filter(item => item.pid === this.id).map(item => item.name)
      console.log(newName)
      if (this.isEdit) {
        // 在所有部门中找到当前点击的部门的那个对象
        const newNamesss = this.orginList.find(item => item.pid === this.id)
        const pid = newNamesss.pid
        // 通过pid找到所有的兄弟同级部门 包括自己
        const brothers = this.orginList.filter(item => item.pid === pid)
        // 在排除自己
        newName = brothers.filter(item => item.id !== this.id).map(item => item.name)
      }
      if (newName.includes(value)) {
        callback(new Error(`部门编码 ${value} 已存在`))
      } else {
        callback()
      }
    }
    return {
      form: {
        name: '', // 部门名称
        code: '', // 部门编码
        manager: '', // 部门管理者
        introduce: ''// 部门介绍
      },
      user: [], // 存放员工信息
      rules: {
        name: [
          { required: true, message: '请输入部门名称', trigger: 'blur' },
          { min: 1, max: 50, message: '请输入长度1~50的字符', trigger: 'blur' },
          { validator: validName, trigger: 'blur' }
        ],
        code: [
          { required: true, message: '请输入部门编码', trigger: 'blur' },
          { min: 1, max: 50, message: '请输入长度1~50的字符', trigger: 'blur' },
          { validator: validCode, trigger: 'blur' }
        ],
        manager: [
          { required: true, message: '请输入部门负责人', trigger: 'blur' }
        ],
        introduce: [
          { required: true, message: '请输入部门介绍', trigger: 'blur' },
          { min: 1, max: 300, message: '请输入长度1~300的字符', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.updateEmployess()
    if (this.isEdit) {
      this.loadDepartments()
    }
  },
  methods: {
    resetForm() {
      this.$refs.deptForm.resetFields()
    },
    // 数据回显 获取点击的部门数据
    async  loadDepartments() {
      const res = await updataDepartments(this.id)
      // console.log(res)
      this.form = res.data
    },
    // 修改部门
    async doEdit() {
      const res = await changeDepartments(this.form)
      console.log(res)
      this.$emit('success')
    },
    submit() {
      // 兜底效验
      this.$refs.deptForm.validate(valid => {
        if (!valid) return
        this.isEdit ? this.doEdit() : this.toAdd()
      })
    },
    // 新增部门
    async toAdd() {
      try {
        const res = await addDepartments({ ...this.form, pid: this.id })
        // console.log(res)
        if (res.code === 10000) {
          // 子传父
          this.$emit('success')
        }
      } catch (error) {
        console.log(error)
      }
    },
    // 获取员工列表信息
    async updateEmployess() {
      try {
        const res = await getEmployess()
        // console.log(res)
        this.user = res.data
      } catch (error) {
        console.log(error)
      }
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
